1 /*
2 * Scope: a generic MVC framework.
3 * Copyright (c) 2000-2002, The Scope team
4 * All rights reserved.
5 *
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * Neither the name "Scope" nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 *
36 * $Id: SAction.java,v 1.4 2002/09/13 17:03:49 ludovicc Exp $
37 */
38 package org.scopemvc.view.swing;
39
40 import java.awt.event.ActionEvent;
41 import javax.swing.AbstractAction;
42 import javax.swing.Action;
43 import javax.swing.Icon;
44
45 import org.apache.commons.logging.Log;
46 import org.apache.commons.logging.LogFactory;
47
48 import org.scopemvc.controller.basic.ViewContext;
49 import org.scopemvc.core.Control;
50 import org.scopemvc.core.Controller;
51 import org.scopemvc.util.ResourceLoader;
52 import org.scopemvc.util.UIStrings;
53
54 /***
55 * A swing Action that issues a Control on action performed. <p>
56 *
57 * SAction uses the control ID and the resources in UIStrings to initialise
58 * itself. <br>
59 * The following properties in SAction are initialised from UIStrings as follow
60 * (replace [Control ID] by the actual value of the controlID property):
61 * <ul>
62 * <li> label is keyed against [Control ID]
63 * <li> shortDescription is keyed against [Control ID].ShortDescription
64 * <li> longDescription is keyed against [Control ID].LongDescription
65 * <li> small icon is keyed against [Control ID].SmallIcon (containing the
66 * path of the icon)
67 * <li> acceleratorKey is keyed against [Control ID].AcceleratorKey
68 * <li> mnemonicKey is keyed against [Control ID].MnemonicKey
69 * </ul>
70 *
71 *
72 * @author <a href="mailto:ludovicc@users.sourceforge.net>Ludovic Claude</a>
73 * @created June 6, 2002
74 * @version 1.0
75 * @see org.scopemvc.util.ResourceLoader for more explanation on own to load
76 * custom icons
77 */
78
79 public class SAction extends AbstractAction implements SwingSubView {
80
81 private static final Log LOG = LogFactory.getLog(SAction.class);
82
83 /***
84 * View that 'owns' this menuitem at any time. When selected, the menuitem
85 * causes its owner to issue a Control.
86 */
87 private SwingView owner;
88
89 private String controlID;
90
91 /***
92 * Constructor for the object
93 */
94 public SAction() {
95 this(null, null);
96 }
97
98 /***
99 * Constructor for the object
100 *
101 * @param inControlID Description of the Parameter
102 */
103 public SAction(String inControlID) {
104 this(inControlID, null);
105 }
106
107
108 /***
109 * Sets text by looking up ControlID in UIStrings.
110 *
111 * @param inControlID issue this Control when user chooses menuitem.
112 * @param inView the parent View that owns this menuitem. This is the view
113 * that will issue a Control when the menuitem is actioned.
114 */
115 public SAction(String inControlID, SwingView inView) {
116 super();
117 setControlID(inControlID);
118 if (inView != null) {
119 setOwner(inView);
120 } else {
121 setEnabled(false);
122 // owner will enable later
123 }
124 }
125
126
127 /***
128 * Gets the control ID
129 *
130 * @return The controlID value
131 */
132 public String getControlID() {
133 return controlID;
134 }
135
136
137 /***
138 * Gets the name
139 *
140 * @return The name value
141 */
142 public String getName() {
143 return (String) getValue(NAME);
144 }
145
146 /***
147 * Gets the small icon
148 *
149 * @return The smallIcon value
150 */
151 public Icon getSmallIcon() {
152 return (Icon) getValue(SMALL_ICON);
153 }
154
155 /***
156 * Gets the accelerator key
157 *
158 * @return The acceleratorKey value
159 */
160 public String getAcceleratorKey() {
161 return (String) getValue(ACCELERATOR_KEY);
162 }
163
164 /***
165 * Gets the long description
166 *
167 * @return The longDescription value
168 */
169 public String getLongDescription() {
170 return (String) getValue(LONG_DESCRIPTION);
171 }
172
173 /***
174 * Gets the short description
175 *
176 * @return The shortDescription value
177 */
178 public String getShortDescription() {
179 return (String) getValue(SHORT_DESCRIPTION);
180 }
181
182 /***
183 * Gets the mnemonic key
184 *
185 * @return The mnemonicKey value
186 */
187 public Integer getMnemonicKey() {
188 return (Integer) getValue(MNEMONIC_KEY);
189 }
190
191 /***
192 * Gets the owner
193 *
194 * @return The owner value
195 */
196 public SwingView getOwner() {
197 return owner;
198 }
199
200
201 /***
202 * Don't assign a Controller to SAction, instead delegate to the containing
203 * SwingView that has a parent Controller.
204 *
205 * @param inControl Description of the Parameter
206 */
207 public void issueControl(Control inControl) {
208 if (getOwner() == null) {
209 LOG.warn("Cannot issue control: Owner not set");
210 } else {
211 SwingUtil.issueControl(getOwner(), inControl);
212 }
213 }
214
215
216 /***
217 * Don't assign a Controller to SAction, instead delegate to the containing
218 * SwingView that has a parent Controller.
219 *
220 * @return The controller value
221 */
222 public Controller getController() {
223 return null;
224 }
225
226 /***
227 * Gets the bound model
228 *
229 * @return The boundModel value
230 */
231 public Object getBoundModel() {
232 return null;
233 }
234
235 /***
236 * Sets the control ID
237 *
238 * @param inControlID The new controlID value
239 */
240 public void setControlID(String inControlID) {
241 controlID = inControlID;
242 if (controlID != null) {
243 if (getName() == null) {
244 String name = UIStrings.get(controlID);
245 setName(name);
246 }
247 if (getSmallIcon() == null) {
248 String iconPath = UIStrings.get(controlID + "." + SMALL_ICON);
249 if (iconPath != null) {
250 try {
251 Icon icon = ResourceLoader.getIcon(iconPath);
252 setSmallIcon(icon);
253 } catch (RuntimeException ignore) {}
254 }
255 }
256 if (getAcceleratorKey() == null) {
257 String acceleratorKey = UIStrings.get(controlID + "." + ACCELERATOR_KEY);
258 setAcceleratorKey(acceleratorKey);
259 }
260 if (getMnemonicKey() == null) {
261 try {
262 Integer mnemonicKey = Integer.valueOf(UIStrings.get(controlID + "." + MNEMONIC_KEY));
263 setMnemonicKey(mnemonicKey);
264 } catch (RuntimeException ignore) {}
265 }
266 if (getShortDescription() == null) {
267 String shortDescription = UIStrings.get(controlID + "." + SHORT_DESCRIPTION);
268 setShortDescription(shortDescription);
269 }
270 if (getLongDescription() == null) {
271 String longDescription = UIStrings.get(controlID + "." + LONG_DESCRIPTION);
272 setLongDescription(longDescription);
273 }
274 }
275 }
276
277
278 /***
279 * Sets the owner
280 *
281 * @param inView The new owner value
282 */
283 public void setOwner(SwingView inView) {
284 owner = inView;
285 setEnabled(owner != null);
286 if (owner != null) {
287 owner.addSubView(this);
288 }
289 }
290
291 /***
292 * Sets the name
293 *
294 * @param value The new name value
295 */
296 public void setName(String value) {
297 putValue(NAME, value);
298 }
299
300 /***
301 * Sets the small icon
302 *
303 * @param value The new smallIcon value
304 */
305 public void setSmallIcon(Icon value) {
306 putValue(SMALL_ICON, value);
307 }
308
309 /***
310 * Sets the accelerator key
311 *
312 * @param value The new acceleratorKey value
313 */
314 public void setAcceleratorKey(String value) {
315 putValue(ACCELERATOR_KEY, value);
316 }
317
318 /***
319 * Sets the long description
320 *
321 * @param value The new longDescription value
322 */
323 public void setLongDescription(String value) {
324 putValue(LONG_DESCRIPTION, value);
325 }
326
327 /***
328 * Sets the short description
329 *
330 * @param value The new shortDescription value
331 */
332 public void setShortDescription(String value) {
333 putValue(SHORT_DESCRIPTION, value);
334 }
335
336 /***
337 * Sets the mnemonic key
338 *
339 * @param value The new mnemonicKey value
340 */
341 public void setMnemonicKey(Integer value) {
342 putValue(MNEMONIC_KEY, value);
343 }
344
345 /***
346 * Don't assign a Controller to this component, instead delegate to the
347 * containing SwingView that has a parent Controller.
348 *
349 * @param inController The new controller value
350 */
351 public void setController(Controller inController) {
352 throw new UnsupportedOperationException("Can't assign a Controller to a " + getClass());
353 }
354
355
356 /***
357 * Sets the bound model
358 *
359 * @param inModel The new boundModel value
360 */
361 public void setBoundModel(Object inModel) {
362 // noop
363 }
364
365 /***
366 * Description of the Method
367 *
368 * @param inView Description of the Parameter
369 */
370 public void unsetOwner(SwingView inView) {
371 if (owner != inView) {
372 return;
373 }
374
375 owner = null;
376 setEnabled(false);
377 }
378
379
380 /***
381 * Description of the Method
382 *
383 * @param inEvent Description of the Parameter
384 */
385 public void actionPerformed(ActionEvent inEvent) {
386 if (controlID != null) {
387 issueControl(createControl());
388 }
389 }
390
391 /***
392 * Override this to create something other than a simple no-parameter
393 * Control.
394 *
395 * @return Control issued when button pressed: here a simple no-parameter
396 * Control
397 */
398 protected Control createControl() {
399 if (controlID == null) {
400 throw new RuntimeException("Can't create a Control because no ControlID set.");
401 }
402
403 return new Control(controlID);
404 }
405
406 }
This page was automatically generated by Maven